< previous page page_454 next page >

Page 454
'
'BODY OF ROUTINE GOES HERE
'
Exit Sub
'
FileCopyError:
'
'ERROR HANDLING ROUTINE GOES HERE (use code from Listing 15.2)
'
End Sub
In addition to handling the anticipated errors, you should also add code that will handle any unexpected errors. This is usually done by adding the Case Else statement to the SelectEnd Select statement group:
Case Else 'Unexpected
    MsgBox(CStr(Err.Number) &  -  & _
        Err.Description, vbOKCancel, App.Title)
Add the error-handling routine to the file copy code and enter an incorrect filename. This will generate an error message telling you that the filename isn't found. You can then re-enter the name and try again. You should also notice that this error routine doesn't use the Resume statement to exit the error handler. The reason for this is that any of the errors that have occurred are all related to the copy operation, and after the error has happened, there is no easy way to continue the copy. It is better to just start over.
Whenever you start adding the error-handling routines to your application, you will quickly see that some code is being repeated over and over again. This will cause problems when you have to change the way a particular error is being handled. In addition, the more code you add to the application, the more complex it will become. With some careful planning, you can reduce the amount of code that you add for error handling by writing a few procedures that the error routines will call to handle the more common error situations.
Moving the error-checking code into a function enables you to reduce the amount of code the program has and simplifies the code that determines if the program can continue. Using the previous code examples, you can see that the filecopy routine calls the error checking routine and uses the return value of the function to determine if it should continue or not:
Private Sub cmdCopy_Click()

On Error GoTo FileCopyError
'
'BODY OF ROUTINE GOES HERE
'
Exit Sub

 
< previous page page_454 next page >

If you like this book, buy it!